﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Test.ModelFactories;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for CommentsRepositoryTest and is intended
    ///to contain all CommentsRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CommentsRepositoryTest : BaseTestClass
    {
        private UsersFactory _usersFactory = new UsersFactory();
        private List<User> _users;
        private PostsFactory _postsFactory = new PostsFactory();
        private Post _post;
        private CommentsFactory _commentsFactory = new CommentsFactory();
        private List<Comment> _comments;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users = _usersFactory.CreateList(3);
            _users.ForEach(user => usersRepository.Add(user));

            var postRepository = _unityContainer.Resolve<IPostsRepository>();
            _post = _postsFactory.CreateEntity(_users[0].RowKey);

            var commentsRepository = _unityContainer.Resolve<ICommentsRepository>();
            _comments = new List<Comment> { 
                _commentsFactory.CreateEntity(_post.RowKey,_users[1].RowKey),
                _commentsFactory.CreateEntity(_post.RowKey,_users[2].RowKey),
            };
            _comments.ForEach(comment => commentsRepository.Add(comment));
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            var postRepository = _unityContainer.Resolve<IPostsRepository>();
            var commentsRepository = _unityContainer.Resolve<ICommentsRepository>();

            usersRepository.DeleteTestEntities();
            postRepository.DeleteTestEntities();
            commentsRepository.DeleteTestEntities();
        }

        /// <summary>
        ///A test for GetCommentsByPost
        ///</summary>
        [TestMethod()]
        public void GetCommentsByPostTest()
        {
            CommentsRepository target = _unityContainer.Resolve<CommentsRepository>();
            string postID = _post.RowKey;
            List<Comment> expected = _comments;
            List<Comment> actual;

            actual = target.GetCommentsByPost(postID);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for DeleteAllPostComments
        ///</summary>
        [TestMethod()]
        public void DeleteAllPostCommentsTest()
        {
            CommentsRepository target = _unityContainer.Resolve<CommentsRepository>();
            string postID = _post.RowKey;

            target.DeleteAllPostComments(postID);
            var actual = target.GetCommentsByPost(postID);
            CollectionAssert.AreEquivalent(new List<Comment>(), actual);
        }

        /// <summary>
        ///A test for DeleteAllUserComments
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserCommentsTest()
        {
            CommentsRepository target = _unityContainer.Resolve<CommentsRepository>();
            string userID = _users[1].RowKey;

            target.DeleteAllUserComments(userID);
            var actual = target.GetCommentsByUser(userID);
            CollectionAssert.AreEquivalent(new List<Comment>(), actual);
        }

        /// <summary>
        ///A test for GetCommentsByUser
        ///</summary>
        [TestMethod()]
        public void GetCommentsByUserTest()
        {
            CommentsRepository target = _unityContainer.Resolve<CommentsRepository>();
            string userID = _users[1].RowKey;
            List<Comment> expected = new List<Comment> { _comments[0] };
            List<Comment> actual;

            actual = target.GetCommentsByUser(userID);
            CollectionAssert.AreEquivalent(expected, actual);
        }
    }
}
